/**
 * Created by cherryzou on 2017/12/11.
 */
import {getProductInfo} from '../api/api.js';
import { Confirm, Alert, Toast, Notify, Loading } from 'vue-ydui/dist/lib.rem/dialog';
export const M={
     getURLParam(){
       var seacher = location.search.slice(1),
         arr = seacher.match(/[^&]+/g), result = {}, arrs = [];
         arr.forEach((item,index)=>{
           arrs=item.match(/[^\=]+/g);
           result[arrs[0]]=arrs[1]
         })
       return result;
     },
    getOpenId(){
       var vm=this,flag=false;
       let para={
         code:vm.getURLParam().Code
       }
      getProductInfo.loginRequest("http://www.kuaixiu.yuxinglake.com/WeiXin.ashx",para).then((res)=>{
          window.localStorage.token=res.access_token;
      })

    },

  addParams(data){
      var arr=[];
      for(var k in data){
        arr.push(k+"="+data[k]);
      }
      return arr.join("&");
  },
  ajax(opts){
    var vm=this;
    var params = opts.type == "get" ? this.addParams(opts.data) : JSON.stringify(opts.data);
    opts = opts || {};
    opts.type = (opts.type || "GET").toUpperCase();
    opts.url=opts.url;
    opts.dataType = opts.dataType || "json";
    opts.contentType = opts.contentType || "application/x-www-form-urlencoded";
    opts.isSet=opts.isSet || true;
    // 1. init
    var xhr=window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject('Microsoft.XMLHTTP');

    //  2. send
    if (opts.type == "GET") {
      xhr.open("GET", opts.url + vm.addParams(opts.data), true);
      xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
      opts.isSet && xhr.setRequestHeader("Authorization", 'bearer '+ window.localStorage.token);
      xhr.send(null);

    } else if (opts.type == "POST") {
      xhr.open("POST", opts.url, true);
      xhr.setRequestHeader('Content-type',opts.contentType);
      opts.isSet && xhr.setRequestHeader("Authorization", 'bearer '+ window.localStorage.token);
      xhr.send(params);
    }
    //3.receive
    xhr.onreadystatechange = function () {
      if (xhr.readyState == 4) {
        var status = xhr.status;
        if (status >= 200 && status < 300) {
          opts.success && opts.success(JSON.parse(xhr.responseText),xhr.responseXML);
        } else {
          opts.fail && opts.fail(status);
        }
      }
    }


  }

}

export const method = {

  setIntervalFun(wait,fn,bFn){
    var i=0,
      run='',
      timer=function(){
        ++i;
        var r=wait- i;
        if(r==-1){
          bFn && bFn()
          window.clearInterval(run);
        }else{
          fn && fn(r);
        }
      },
      run=window.setInterval(timer,1000);
  },
  isMobile () {
    return (navigator.userAgent.match(/Win/i)
      || navigator.userAgent.match(/MacIntel/i)
      || navigator.userAgent.match(/Linux/i)
    ) ? false : true;
  },
  isPhone() {
    return /(iPhone|iPod|iPad);?/i.test(navigator.userAgent);
  },
  isAndriod() {
    return /android/i.test(navigator.userAgent);
  }
}
export const regTest={

  testMobile(val,errorFn){
    var flag=false, vm=this;
    if(!val){
      errorFn("手机号码不能为空");
      return !1;
    }
    if(val.length>11){
      val=val.substring(0,11)
    }
    let rTel=/^(13|14|15|17|18)+(\d{9})$/g;
    if(rTel.test(val)){
      flag=true;
    }else{
      errorFn('请输入11位数的手机号码');

    }
    return flag;
  },
  testIDCard(value,errorFn){
    if(!value){
      errorFn("身份证号码不能为空");
      return !1;
    }
    let isID=/^\d{17}(\d|x)$/i;
    let aCity={11:"北京",12:"天津",13:"河北",14:"山西",15:"内蒙古",21:"辽宁",22:"吉林",23:"黑龙江",31:"上海",32:"江苏",33:"浙江",
      34:"安徽",35:"福建",36:"江西",37:"山东",41:"河南",42:"湖北",43:"湖南",44:"广东",45:"广西",46:"海南",50:"重庆",
      51:"四川",52:"贵州",53:"云南",54:"西藏",61:"陕西",62:"甘肃",63:"青海",64:"宁夏",65:"新疆",71:"台湾",81:"香港",82:"澳门",91:"国外"
    };

    let cBirth=[value.substr(6,4),value.substr(10,2),value.substr(12,2)].join('-');
    let d=new Date(cBirth.replace(/-/g,"/"));
    let dates=d.getDate()<10 ? "0"+d.getDate() :d.getDate();
    let mon=(d.getMonth()+1)<10 ? "0"+(d.getMonth()+1) :(d.getMonth()+1);
    let cDate=[d.getFullYear(), mon, dates].join("-");
    var  flag=false;

    var sNum=0;

    for(var i=0; i<18; i++){
      sNum+= (Math.pow(2,i) % 11) * parseInt(value.charAt(17 - i),11);
    }
    if(!isID.test(value)){
      value=value.substring(0,17);
      errorFn('身份证号码需要为18位');
    }else if(aCity[parseInt(value.substr(0,2))]==null){
      errorFn('您的身份证地区代码不合法');
    }else if(cBirth!=cDate){

      errorFn('身份证上的出生日期非法');
    }else if(sNum%11!=1){
      errorFn('你输入的身份证号非法');
    }else{
      flag=true;
    }
    return flag;

  }
}
